#include <os/atomic.h>
#include "include/FreeRTOS.h"


/**
 * @brief Set the atomic value
 * @param v atomic_t
 * @param i int
 * @return none
 */
void atomic_set(atomic_t *v, int i)
{
    portENTER_CRITICAL();
    v->counter = i;
    portEXIT_CRITICAL();
}


/**
 * @brief Get the atomic value
 * @param v atomic_t
 * @return int
 */
int atomic_read(atomic_t *v)
{
    portENTER_CRITICAL();
    int i = v->counter;
    portEXIT_CRITICAL();

    return i;
}


/**
 * @brief Add the atomic value
 * @param v atomic_t
 * @param i int
 * @return none
 */
void atomic_add(atomic_t *v, int i)
{
    portENTER_CRITICAL();
    v->counter += i;
    portEXIT_CRITICAL();
}


/**
 * @brief Sub the atomic value
 * @param v atomic_t
 * @param i int
 * @return none
 */
void atomic_sub(atomic_t *v, int i)
{
    portENTER_CRITICAL();
    v->counter -= i;
    portEXIT_CRITICAL();
}


/**
 * @brief Inc the atomic value
 * @param v atomic_t
 * @return int
 * @return none
 */
void atomic_inc(atomic_t *v)
{
    portENTER_CRITICAL();
    v->counter ++;
    portEXIT_CRITICAL();
}


/**
 * @brief Dec the atomic value
 * @param v atomic_t
 * @return int
 */
void atomic_dec(atomic_t *v)
{
    portENTER_CRITICAL();
    v->counter --;
    portEXIT_CRITICAL();
}
